﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system
{
    public class storageadj_product_class
    {
        DataTable dtList;

        string connectionString;

        public decimal Total_qty1 { get; set; }
        public decimal Total_qty2 { get; set; }

        private void getTotla()
        {
            Total_qty1 = 0;
            Total_qty2 = 0;

            //统计 数量合计， 金额， 单价
            foreach (DataRow dr in dtList.Rows)
            {
                Total_qty1 = Total_qty1 + Decimal.Parse(dr["qty1"].ToString());
                Total_qty2 = Total_qty2 + Decimal.Parse(dr["qty2"].ToString());
            }
        }

        public storageadj_product_class(string connectionString)
        {
            this.connectionString = connectionString;

            dtList = new DataTable();

            dtList.Columns.Add("id");
            dtList.Columns.Add("areaid_out");
            dtList.Columns.Add("storageid_out");
            dtList.Columns.Add("areaname_out");
            dtList.Columns.Add("storagename_out");
            dtList.Columns.Add("areaid_in");
            dtList.Columns.Add("storageid_in");
            dtList.Columns.Add("areaname_in");
            dtList.Columns.Add("storagename_in");
            dtList.Columns.Add("productcode");
            dtList.Columns.Add("productname");
            dtList.Columns.Add("productshortname");
            dtList.Columns.Add("goodsclassname");
            dtList.Columns.Add("goodsunitname");
            dtList.Columns.Add("goodsspecname");
            dtList.Columns.Add("stackname");
            dtList.Columns.Add("guardname");
            dtList.Columns.Add("qty1");
            dtList.Columns.Add("qty2");
        }

        public bool Add(string areaid_out, string storageid_out, string areaid_in, string storageid_in, 
                        string productcode, string qty1, string qty2, ref string err_msg)
        {
            err_msg = "";

            decimal _qty1, _qty2;

            if (String.IsNullOrEmpty(areaid_out))
            {
                err_msg = Errormsg.purchasein_putin_Area_IsNullOrEmpty;
                return false;
            }

            if (String.IsNullOrEmpty(storageid_out))
            {
                err_msg = Errormsg.purchasein_putin_Storage_IsNullOrEmpty;
                return false;
            }

            if (String.IsNullOrEmpty(areaid_in))
            {
                err_msg = Errormsg.purchasein_putin_Area_IsNullOrEmpty;
                return false;
            }

            if (String.IsNullOrEmpty(storageid_in))
            {
                err_msg = Errormsg.purchasein_putin_Storage_IsNullOrEmpty;
                return false;
            }

            if (String.IsNullOrEmpty(productcode))
            {
                err_msg = Errormsg.purchaseplan_product_IsNullOrEmpty;
                return false;
            }

            if (Decimal.TryParse(qty1, out _qty1) == false)
            {
                err_msg = Errormsg.purchaseplan_product_QtyFormatError;
                return false;
            }

            if (Decimal.TryParse(qty2, out _qty2) == false)
            {
                err_msg = Errormsg.purchaseplan_product_QtyFormatError;
                return false;
            }

            //检查是否已存在相同的货品条码            
            if (dtList.Select(String.Format("productcode='{0}' and storageid_out='{1}' and storageid_in='{2}'", productcode, storageid_out, storageid_in)).Length > 0)
            {
                err_msg = Errormsg.purchaseplan_product_ExistProduct;
                return false;
            }

            //构造SQL语句
            string sql = @"select  '{1}' areaid_out, 
                                   (select areaname from area where areaid='{1}') areaname_out,
                                   '{2}' storageid_out,
                                   (select storagename from storage where storageid='{2}') storagename_out,
                                   '{3}' areaid_in, 
                                   (select areaname from area where areaid='{3}') areaname_in,
                                   '{4}' storageid_in,
                                   (select storagename from storage where storageid='{4}') storagename_in,
                                   a.productcode, a.productname, a.productshortname, a.goodsclassid, a.goodsunitid, a.goodsspecid, a.stackid, a.guardid, a.maxqty, a.minqty, a.sysdate,
                                   a.goodsclassname, a.goodsunitname, a.goodsspecname, a.stackname, a.guardname
                              from  v_product a
                                where a.productcode = '{0}'";

            sql = String.Format(sql, productcode, areaid_out, storageid_out, areaid_in, storageid_in);

            Execute exec = new Execute(connectionString);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref err_msg);

            if((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count != 1))
            {
                err_msg = Errormsg.purchaseplan_product_NotFoundProduct;
                return false;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                DataRow newdr = dtList.NewRow();

                newdr["id"] = (DateTime.Now.Ticks + dtList.Rows.Count).ToString();

                newdr["areaid_out"] = dr["areaid_out"].ToString();
                newdr["storageid_out"] = dr["storageid_out"].ToString();
                newdr["areaname_out"] = dr["areaname_out"].ToString();
                newdr["storagename_out"] = dr["storagename_out"].ToString();

                newdr["areaid_in"] = dr["areaid_in"].ToString();
                newdr["storageid_in"] = dr["storageid_in"].ToString();
                newdr["areaname_in"] = dr["areaname_in"].ToString();
                newdr["storagename_in"] = dr["storagename_in"].ToString();

                newdr["productcode"] = dr["productcode"].ToString();
                newdr["productname"] = dr["productname"].ToString();
                newdr["productshortname"] = dr["productshortname"].ToString();
                newdr["goodsclassname"] = dr["goodsclassname"].ToString();
                newdr["goodsunitname"] = dr["goodsunitname"].ToString();
                newdr["goodsspecname"] = dr["goodsspecname"].ToString();
                newdr["stackname"] = dr["stackname"].ToString();
                newdr["guardname"] = dr["guardname"].ToString();
                newdr["qty1"] = _qty1;
                newdr["qty2"] = _qty2;

                dtList.Rows.Add(newdr);
            }

            getTotla();

            return true;
        }

        public void Reset()
        {
            dtList.Rows.Clear();

            getTotla();
        }

        public void Delect(string id)
        {
            DataRow[] drFind = dtList.Select(String.Format("id='{0}'", id));

            foreach (DataRow dr in drFind)
            {
                dtList.Rows.Remove(dr);
            }

            getTotla();
        }

        public DataTable getList()
        {
            return this.dtList;
        }

        public int getListTotal()
        {
            return dtList.Rows.Count;
        }

        public bool getDataList(string id)
        {
            string err_msg = "";

            dtList.Rows.Clear();

            //构造SQL语句
            string sql = @"select a.areaid_out, 
                                  a.storageid_out,
                                  (select area.areaname from area where a.areaid_out=area.areaid) areaname_out,
                                  (select storage.storagename from storage where a.storageid_out=storage.storageid) storagename_out,
                                  a.areaid_in, 
                                  a.storageid_in,
                                  (select area.areaname from area where a.areaid_in=area.areaid) areaname_in,
                                  (select storage.storagename from storage where a.storageid_in=storage.storageid) storagename_in,
                                  b.productcode, b.productname, b.productshortname, 
                                  b.goodsclassname, b.goodsunitname, b.goodsspecname, b.stackname, b.guardname, 
                                  a.qty
                              from Library_adj_detail a, v_product b
                               where a.productcode=b.productcode
                                 and a.ljid='{0}'";

            sql = String.Format(sql, id);

            Execute exec = new Execute(connectionString);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref err_msg);

            if (ds == null)
            {
                return false;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                DataRow newdr = dtList.NewRow();

                newdr["id"] = (DateTime.Now.Ticks + dtList.Rows.Count).ToString();

                newdr["areaid_out"] = dr["areaid_out"].ToString();
                newdr["storageid_out"] = dr["storageid_out"].ToString();
                newdr["areaname_out"] = dr["areaname_out"].ToString();
                newdr["storagename_out"] = dr["storagename_out"].ToString();

                newdr["areaid_in"] = dr["areaid_in"].ToString();
                newdr["storageid_in"] = dr["storageid_in"].ToString();
                newdr["areaname_in"] = dr["areaname_in"].ToString();
                newdr["storagename_in"] = dr["storagename_in"].ToString();

                newdr["productcode"] = dr["productcode"].ToString();
                newdr["productname"] = dr["productname"].ToString();
                newdr["productshortname"] = dr["productshortname"].ToString();
                newdr["goodsclassname"] = dr["goodsclassname"].ToString();
                newdr["goodsunitname"] = dr["goodsunitname"].ToString();
                newdr["goodsspecname"] = dr["goodsspecname"].ToString();
                newdr["stackname"] = dr["stackname"].ToString();
                newdr["guardname"] = dr["guardname"].ToString();
                newdr["qty1"] = dr["qty"].ToString(); 
                newdr["qty2"] = "0";

                dtList.Rows.Add(newdr);
            }

            getTotla();

            return true;
        }
    }
}